﻿using Models.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Web.Common;
using Web.Filters;

namespace Web.Controllers.API
{
    [ApiAuthentication]
    public class BasicInfoController : ApiController
    {
        [HttpPost]
        public IHttpActionResult GoodsSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            SqlParameter[] parameters = {
                                            new SqlParameter("@PageIndex",SqlDbType.Int),
                                            new SqlParameter("@PageSize",SqlDbType.Int),
                                            new SqlParameter("@TypeID",SqlDbType.Int),
                                            new SqlParameter("@State",SqlDbType.Int),
                                            new SqlParameter("@Search",SqlDbType.NVarChar,40000),
                                            new SqlParameter("@Type",SqlDbType.Int),
                                            new SqlParameter("@BillNameID",SqlDbType.Int)};
            parameters[0].Value = entity.PageIndex;
            parameters[1].Value = entity.PageSize;
            parameters[2].Value = entity.TypeID;
            parameters[3].Value = 1;
            parameters[4].Value = entity.Search;
            parameters[5].Value = 0;
            parameters[6].Value = entity.BillNameID;
            DataSet ds = FreeADO.DBUtility.DbHelperSQL.RunProcedure("SP_QueryGoodsSelect", parameters, "aaa", 3000);
            result.Data = ds.Tables[1];
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult UserSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            int did = 0;
            switch (entity.BillNameID)
            {
                case 1:
                case 2:
                    did = 1;
                    break;
                case 3:
                    did = 2;
                    break;
                case 4:
                    did = 3;
                    break;
                case 5:
                    did = 4;
                    break;
                case 6:
                case 7:
                    did = 5;
                    break;
                default:
                    break;
            }

            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Users where DepartmentID=" + did + " and IsDeleted=0").Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult BillNameSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            string IDs = "0";
            if (Web.Common.Common.IsAuthoritys(56) > 0)
            {
                IDs = IDs + ",1";
            }
            if (Web.Common.Common.IsAuthoritys(57) > 0)
            {
                IDs = IDs + ",2";
            }
            if (Web.Common.Common.IsAuthoritys(58) > 0)
            {
                IDs = IDs + ",3";
            }
            if (Web.Common.Common.IsAuthoritys(59) > 0)
            {
                IDs = IDs + ",4";
            }
            if (Web.Common.Common.IsAuthoritys(60) > 0)
            {
                IDs = IDs + ",5";
            }
            if (Web.Common.Common.IsAuthoritys(61) > 0)
            {
                IDs = IDs + ",6";
            }
            if (Web.Common.Common.IsAuthoritys(62) > 0)
            {
                IDs = IDs + ",7";
            }
            if (Web.Common.Common.IsAuthoritys(103) > 0)
            {
                IDs = IDs + ",8";
            }
            if (Web.Common.Common.IsAuthoritys(104) > 0)
            {
                IDs = IDs + ",9";
            }
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillName where ID in (" + IDs + ")").Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult ProceduresSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from Procedures where IsDeleted=0 and State=1 and BillNameID="+entity.BillNameID).Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult BillTempletIndexSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from BillTempletIndex where IsDeleted=0 and BillNameID=" + entity.BillNameID).Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult GongYiSelect([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            Result<DataTable> result = new Result<DataTable>();
            result.Type = AjaxResultType.Success;
            result.Content = "成功";
            DataTable dt = FreeADO.DBUtility.DbHelperSQL.Query("select * from GongYi where ProcedureID=" + entity.ProcedureID + " and IsDeleted=0").Tables[0];
            result.Data = dt;
            return Json(result);
        }
        [HttpPost]
        public IHttpActionResult IsAuthoritys([FromBody]Models.API.Param.BasicInfoModel entity)
        {
            try
            {
                Result<bool> result = new Result<bool>() { Type = AjaxResultType.Success, Content = "成功", TotalRows = 0, Data = false };
                int UserID = BaseWebApiExt.GetUserIDByToken();
                result.Data = Convert.ToInt32(FreeADO.DBUtility.DbHelperSQL.GetSingle(@"  select count(*)
  from RoleAuthority as a inner join Roles as b on a.RoleID=b.ID inner join UserRoles as c on b.ID=c.RoleID
  where c.UserID=" + UserID + " and a.AuthorityID=" + entity.AuthorityID)) > 0;

                return Json(result);
            }
            catch (Exception e)
            {
                Result<bool> result = new Result<bool>() { Type = AjaxResultType.Error, Content = e.Message, TotalRows = 0, Data = false };
                return Json(result);
            }
        }

    }
}